/*** Eclipse Class Decompiler plugin, copyright (c) 2016 Chen Chao (cnfree2000@hotmail.com) ***/
package shop.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDao {
	public static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	public static final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=ShopSystem";
	public static final String DBNAME = "sa";
	public static final String DBPASS = "123456";

	public Connection getConn() throws ClassNotFoundException, SQLException {
		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=ShopSystem", "sa",
				"123456");
		return conn;
	}

	public void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException arg6) {
				arg6.printStackTrace();
			}
		}

		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException arg5) {
				arg5.printStackTrace();
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException arg4) {
				arg4.printStackTrace();
			}
		}

	}

	public int executeSQL(String preparedSql, String[] param) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		int num = 0;

		try {
			conn = this.getConn();
			pstmt = conn.prepareStatement(preparedSql);
			if (param != null) {
				for (int e = 0; e < param.length; ++e) {
					pstmt.setString(e + 1, param[e]);
				}
			}

			num = pstmt.executeUpdate();
		} catch (ClassNotFoundException arg10) {
			arg10.printStackTrace();
		} catch (SQLException arg11) {
			arg11.printStackTrace();
		} finally {
			this.closeAll(conn, pstmt, (ResultSet) null);
		}

		return num;
	}
}